package com.eastedu.superise.handler.basic;

import com.eastedu.manager.FileManager;
import com.eastedu.model.basic.ResourceFile;
import com.eastedu.repository.basic.po.BasicResourcePO;
import com.eastedu.superise.handler.AbstractHistoricalDataHandler;
import com.eastedu.utils.JsonUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 【解冻】文件归档
 * 冷存储范围：每年9月份，将五年前的 标红资源进行冷存处理，五年内的保持原来的存储方案, 排查幼儿园
 * 故事ID 90690
 *
 * @author zhangzhishuai
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class BasicFileRestoreHistoricalDataHandler extends AbstractHistoricalDataHandler<BasicResourcePO> {

    private final FileManager fileManager;


    @Override
    protected String getSql() {
        return "SELECT * FROM t_basic_resource WHERE create_time < 1567267200000 and category in( 1, 8, 7) AND study_section_code = 'XQ'";
    }

    @Override
    public void callback(BasicResourcePO po) throws SQLException {
        Set<String> fileIds = JsonUtil.parseList(po.getFilesJson(), ResourceFile.class)
                .stream()
                .filter(resourceFile -> "mp4".equalsIgnoreCase(resourceFile.getExtension()))
                .map(ResourceFile::getFileId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        try {
            fileManager.restore(new ArrayList<>(fileIds));
        } catch (Exception e) {
            log.error("{}:查询文件信息异常：{}", fileIds, e.getMessage(), e);
        }
    }

}
